<?php
/**
 * Created by Jasmine.
 * Date: 2016/5/30
 * description:
 * Address: 浙江宣逸网络科技有限公司
 */

namespace Wap\Controller;


use Common\Model\PointLotteryModel;
use Wap\BaseController;

class PointLotteryController extends BaseController{

    /**
     * 积分抽奖首页
     * author: wwl<wuwl@soe-soe.com>
     * Return: void
     * Address: 浙江宣逸网络科技有限公司
     */
    public function index(){
        $uid = session("uid")+0;
        $page = I('get.page',1,'intval');
        $status = I('get.status',1,'intval');
        $lotteryInfo = PointLotteryModel::getLotteryList($uid,$status,$page,8);
        //是否当日首次签到
        signAssignWap($uid,$this->smarty);

        $this->smarty->assign('data',$lotteryInfo['list']);
        $this->smarty->assign('title','积分抽奖');
        $this->smarty->assign('status',$status);
        $this->smarty->display('PointLottery/index.html');

    }
    //积分抽奖首页加载更多
    public function indexMore(){
        $uid = session("uid")+0;
        $page = I('get.page',2,'intval');
        $status = I('get.status',1,'intval');
        $lotteryInfo = PointLotteryModel::getLotteryList($uid,$status,$page,8);

        $this->smarty->assign('data',$lotteryInfo['list']);
        $this->smarty->assign('status',$status);
        $html = $this->smarty->fetch('PointLottery/indexMore.html');
        $this->ajaxReturn(array('status' =>0,'dataHtml' =>$html,'isPage' =>$lotteryInfo['nextPage']));
    }

    //积分抽奖在线活动详情
    public function detail(){
        $uid = session('uid')+0;
        $id = I('get.id',0,'intval');
        if(!$id){
            redirect(U('PointLottery/index'));//数据为空返回列表
        }
        $status = I('get.status',1,'intval');

        $data = PointLotteryModel::getLotteryData($id,$status,$uid);

        if($data['data']['finishType']==PointLotteryModel::POINTLOTTERY_TIMING){
            if($status==PointLotteryModel::LOTTERY_ONLINE){
                $this->smarty->assign('title','定时抽奖-进行中');
            }else if($status==PointLotteryModel::LOTTERY_PREPARE){
                $this->smarty->assign('title','定时抽奖-未开始');
            }else if($status==PointLotteryModel::LOTTERY_FINISH){
                $this->smarty->assign('title','定时抽奖-已结束');
            }

        }else{
            if($status==PointLotteryModel::LOTTERY_ONLINE){
                $this->smarty->assign('title','满员抽奖-进行中');
            }else if($status==PointLotteryModel::LOTTERY_PREPARE){
                $this->smarty->assign('title','满员抽奖-未开始');
            }else if($status==PointLotteryModel::LOTTERY_FINISH){
                $this->smarty->assign('title','定时抽奖-已结束');
            }
            $remainder = $data['data']['fullNumber']-$data['data']['joinPeople'];
            $this->smarty->assign('remainder',$remainder);
        }

        //是否当日首次签到
        signAssignWap($uid,$this->smarty);

        $marquee = explode(',',$data['data']['marqueePic']);
        $this->smarty->assign('marquee',$marquee);
        $this->smarty->assign('data',$data['data']);
        $this->smarty->assign('status',$status);
        $this->smarty->assign('flag',$data['flag']);
        $this->smarty->display('PointLottery/detail.html');

    }
    /**
     * 抽奖操作
     * @param type:1:定时抽奖；2：满员抽奖
     * author: wwl <wuwl@soe-soe.com>
     * Address: 浙江宣逸网络科技有限公司
     */
    public function doExchange(){
        $lotteryID = I('post.lotteryID',0,'intval');
        $uid = session('uid')+0;
        if($this->isWechat){
            if(!session('bindPhone')){
                $this->ajaxReturn(array('status'=>3,'msg'=>'请绑定手机号码','url'=>urlencode(U('PointLottery/detail')."&id=".$lotteryID."&status=1")));
            }
        } else{
            if(!$uid){
                $this->ajaxReturn(array('status'=>2,'msg'=>'请登录后操作','url'=>urlencode(U('PointLottery/detail')."&id=".$lotteryID."&status=1")));
            }
        }
        $type = I('post.type',1,'intval');

        $exchangeNum = I('post.exchangeNum',1,'intval');
        $requirePoint = I('post.requirePoint',0,'intval');
        $userPoint = session('point');

        if($userPoint < $requirePoint){
            $this->ajaxReturn(array('status'=>1,'msg'=>'您的积分不足'));
        }

        $isEnd = PointLotteryModel::isEnd($lotteryID);
        if($isEnd==1){
            if($type==PointLotteryModel::POINTLOTTERY_TIMING) {
                $requireTotalPoint = $requirePoint*$exchangeNum;
                $status = PointLotteryModel::timingExchange($uid,$type,$lotteryID,$exchangeNum,$requireTotalPoint);
            }
            else $status = PointLotteryModel::fullExchange($uid,$type,$lotteryID,$exchangeNum,$requirePoint);
        } else{
            $status['status']=1;
            $status['msg']="sorry,该活动已结束，去看看别的吧";
        }
        $this->ajaxReturn($status);
    }
    /**
     * 我的积分抽奖记录
     * author: wwl <wuwl@soe-soe.com>
     * Return: void
     * Address: 浙江宣逸网络科技有限公司
     */
    public function myRecord(){
        $uid = session('uid')+0;
        if(!$uid){
            redirect(U('User/login')."&uri=".urlencode($_SERVER['REQUEST_URI']));
        }
        $page = I('get.page',1,'intval');
        $data = PointLotteryModel::myRecord($uid,$page,8);

        //是否当日首次签到
        signAssignWap($uid,$this->smarty);

        $this->smarty->assign('data',$data['data']);
        $this->smarty->assign('title','兑换记录');
        $this->smarty->display('PointLottery/myRecord.html');
    }
    //我的抽奖记录 加载更多
    public function recordMore(){
        $uid = session('uid')+0;
        $page = I('get.page',2,'intval');

        $data = PointLotteryModel::myRecord($uid,$page,8);

        $this->smarty->assign('data',$data['data']);
        $html = $this->smarty->fetch('PointLottery/recordMore.html');
        $this->ajaxReturn(array('status' =>0,'dataHtml' =>$html,'isPage' =>$data['nextPage']));
    }

    //满员抽奖玩法介绍
    public function fullIntroduce(){
        $uid = session("uid")+0;
        //是否当日首次签到
        signAssignWap($uid,$this->smarty);

        $this->smarty->assign('title','满员抽奖-玩法介绍');
        $this->smarty->display('PointLottery/fullIntroduce.html');
    }
    //积分抽奖玩法介绍
    public function timingIntroduce(){
        $uid = session("uid")+0;
        //是否当日首次签到
        signAssignWap($uid,$this->smarty);

        $this->smarty->assign('title','定时抽奖-玩法介绍');
        $this->smarty->display('PointLottery/timingIntroduce.html');
    }
}